<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-reverb" tabindex="-1"><a class="header-anchor" href="#laravel-reverb"><span>Laravel Reverb</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#installation">安装</a></li>
<li><a href="#configuration">配置</a>
<ul>
<li><a href="#application-credentials">应用凭据</a></li>
<li><a href="#allowed-origins">允许的来源</a></li>
<li><a href="#additional-applications">多应用</a></li>
<li><a href="#ssl">SSL</a></li>
</ul>
</li>
<li><a href="#running-server">运行服务</a>
<ul>
<li><a href="#debugging">调试</a></li>
<li><a href="#restarting">重启</a></li>
</ul>
</li>
<li><a href="#monitoring">监控</a></li>
<li><a href="#production">生产环境下运行服务</a>
<ul>
<li><a href="#open-files">打开的文件</a></li>
<li><a href="#event-loop">事件循环</a></li>
<li><a href="#web-server">Web 服务</a></li>
<li><a href="#ports">端口</a></li>
<li><a href="#process-management">进程管理</a></li>
<li><a href="#scaling">扩展</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p><a href="https://github.com/laravel/reverb" target="_blank" rel="noopener noreferrer">Laravel Reverb</a> 为你的 Laravel 应用带来了快速的、可扩展的实时 WebSocket 通信, 并提供与 Laravel 现有<a href="https://learnku.com/docs/laravel/11.x/broadcasting" target="_blank" rel="noopener noreferrer">事件广播工具套件</a>的无缝集成。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>你可以使用 <code v-pre>install:broadcasting</code> Artisan 命令安装 Reverb：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan install<span class="token punctuation">:</span>broadcasting</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<p>在底层，<code v-pre>install:broadcasting</code> Artisan 命令会运行 <code v-pre>reverb:install</code> 命令，它会使用一系列合理的默认配置选项来安装 Reverb。如果你想要更改任何配置选项，可以通过更新 Reverb 的环境变量或者更新 <code v-pre>config/reverb.php</code> 配置文件来进行。</p>
<h3 id="应用凭据" tabindex="-1"><a class="header-anchor" href="#应用凭据"><span>应用凭据</span></a></h3>
<p>为了建立与 Reverb 的连接，必须在客户端和服务器之间交换一组 Reverb 「应用程序」凭据。这些凭据在服务器上配置，用于验证来自客户端的请求。你可以使用以下环境变量定义这些凭据：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token key attr-name">REVERB_APP_ID</span><span class="token punctuation">=</span><span class="token value attr-value">my-app-id</span></span>
<span class="line"><span class="token key attr-name">REVERB_APP_KEY</span><span class="token punctuation">=</span><span class="token value attr-value">my-app-key</span></span>
<span class="line"><span class="token key attr-name">REVERB_APP_SECRET</span><span class="token punctuation">=</span><span class="token value attr-value">my-app-secret</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="允许的来源" tabindex="-1"><a class="header-anchor" href="#允许的来源"><span>允许的来源</span></a></h3>
<p>您还可以通过更新<code v-pre>config/reverb.php</code>配置文件的<code v-pre>apps</code>部分中的<code v-pre>allowed_origins</code>配置值的值来定义客户端请求的来源。任何未列在允许来源中的请求都会被拒绝。 您可以使用 <code v-pre>*</code> 来允许所有来源:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apps'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'my-app-id'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'allowed_origins'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'laravel.com'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="多应用" tabindex="-1"><a class="header-anchor" href="#多应用"><span>多应用</span></a></h3>
<p>通常情况下，Reverb 为安装它的应用提供 WebSocket 服务。然而，也有可能通过单个 Reverb 安装来为多个应用提供服务。</p>
<p>例如，您可能希望维护一个单一的 Laravel 应用，通过 Reverb 为多个应用提供 WebSocket 连接。这可以通过在应用的 <code v-pre>config/reverb.php</code> 配置文件中定义多个 apps 来实现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apps'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'app_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'my-app-one'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'app_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'my-app-two'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="ssl" tabindex="-1"><a class="header-anchor" href="#ssl"><span>SSL</span></a></h3>
<p>在大多数情况下，在将请求代理到 Reverb 服务器之前，安全的 WebSocket 连接由上游 Web 服务器（Nginx 等）处理。</p>
<p>但是在本地开发，直接使用 Reverb 服务处理连接是比较有用的。 如果您正在使用 <a href="https://herd.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Herd’s</a> 安全站点亦或是 <a href="https://learnku.com/docs/laravel/11.x/valet" target="_blank" rel="noopener noreferrer">Laravel Valet</a> 并且您的应用已经运行 <a href="https://learnku.com/docs/laravel/11.x/valet#securing-sites" target="_blank" rel="noopener noreferrer">secure command</a> 命令， 您可以使用为您的站点生成的 Herd / Valet 证书来保护您的 Reverb 连接。 为此，设置 <code v-pre>REVERB_HOST</code> 环境变量为您站点的主机名或者在启动 Reverb 服务器时明确传递主机名选项：:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan reverb:start <span class="token parameter variable">--host</span><span class="token operator">=</span><span class="token string">"0.0.0.0"</span> <span class="token parameter variable">--port</span><span class="token operator">=</span><span class="token number">8080</span> <span class="token parameter variable">--hostname</span><span class="token operator">=</span><span class="token string">"laravel.test"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>因为 Herd 和 Valet 域指向 <code v-pre>localhost</code>，执行上述命令可以使你的 Reverb 服务可以通过安全的 WebSocket 协议 (<code v-pre>wss</code>) 在 <code v-pre>wss://laravel.test:8080</code> 上进行访问。</p>
<p>你还可以通过在应用的 <code v-pre>config/reverb.php</code> 配置文件中定义 <code v-pre>tls</code> 选项来手动选择证书。 在 <code v-pre>tls</code> 选项数组中，你可以提供 <a href="https://www.php.net/manual/en/context.ssl.php" target="_blank" rel="noopener noreferrer">PHP 的 SSL 上下文选项</a> 支持的任意选项:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'tls'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'local_cert'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'/path/to/cert.pem'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="运行服务器" tabindex="-1"><a class="header-anchor" href="#运行服务器"><span>运行服务器</span></a></h2>
<p>Reverb 服务可以使用 <code v-pre>reverb:start</code> Artisan 命令来启动：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan reverb:start</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，Reverb 服务会在 <code v-pre>0.0.0.0:8080</code> 启动，使其能够从所有网络接口进行访问。</p>
<p>如果你需要指定自定义主机或端口，你可以在启动服务时通过 <code v-pre>--host</code> 和 <code v-pre>--port</code> 选项来这么做：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan reverb:start <span class="token parameter variable">--host</span><span class="token operator">=</span><span class="token number">127.0</span>.0.1 <span class="token parameter variable">--port</span><span class="token operator">=</span><span class="token number">9000</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，你可以在应用的 <code v-pre>.env</code> 配置文件中定义 <code v-pre>REVERB_SERVER_HOST</code> 和 <code v-pre>REVERB_SERVER_PORT</code> 环境变量。</p>
<p>不要把 <code v-pre>REVERB_SERVER_HOST</code> 和 <code v-pre>REVERB_SERVER_PORT</code> 环境变量与 <code v-pre>REVERB_HOST</code> 和 <code v-pre>REVERB_PORT</code> 弄混了。前者指定运行 Reverb 服务本身的主机和端口，而后一对则指示 Laravel 在何处发送广播消息。例如，在生产环境中，你可以将请求从端口 <code v-pre>443</code> 上的公共 Reverb 主机名路由到在 <code v-pre>0.0.0.0:8080</code> 上运行的 Reverb 服务器。在此方案中，环境变量的定义如下：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token key attr-name">REVERB_SERVER_HOST</span><span class="token punctuation">=</span><span class="token value attr-value">0.0.0.0</span></span>
<span class="line"><span class="token key attr-name">REVERB_SERVER_PORT</span><span class="token punctuation">=</span><span class="token value attr-value">8080</span></span>
<span class="line"></span>
<span class="line"><span class="token key attr-name">REVERB_HOST</span><span class="token punctuation">=</span><span class="token value attr-value">ws.laravel.com</span></span>
<span class="line"><span class="token key attr-name">REVERB_PORT</span><span class="token punctuation">=</span><span class="token value attr-value">443</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="调试" tabindex="-1"><a class="header-anchor" href="#调试"><span>调试</span></a></h3>
<p>为了提升性能，Reverb 默认不会输出任何调试信息。如果你想要看到通过 Reverb 服务传播的数据流，你可以在 <code v-pre>reverb:start</code> 命令中提供 <code v-pre>--debug</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan reverb:start <span class="token parameter variable">--debug</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="重启" tabindex="-1"><a class="header-anchor" href="#重启"><span>重启</span></a></h3>
<p>由于 Reverb 是一个长期运行的过程，不重启服务器的话，你的代码变更将不会生效。可以通过 <code v-pre>reverb:restart</code> Artisan 命令来重启服务。</p>
<p><code v-pre>reverb:restart</code> 命令确保在停止服务器之前所有连接都优雅地终止。如果你使用如 Supervisor 这样的进程管理器运行 Reverb，服务器将在所有连接被终止后由进程管理器自动重启：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan reverb:restart</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="监控" tabindex="-1"><a class="header-anchor" href="#监控"><span>监控</span></a></h2>
<p>Reverb 的监控可以集成在 <a href="https://learnku.com/docs/laravel/11.x/pulse" target="_blank" rel="noopener noreferrer">Laravel Pulse</a>。 启用 Reverb 的 Pulse 集成后，你可以追踪服务器处理的连接数和消息数。</p>
<p>为了启用集成，你首先应该确保你已经 <a href="https://learnku.com/docs/laravel/11.x/pulse#installation" target="_blank" rel="noopener noreferrer">安装了 Pulse</a>。 然后，将 Reverb 的任何记录器添加到应用的 <code v-pre>config/pulse.php</code> 配置文件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Reverb<span class="token punctuation">\</span>Pulse<span class="token punctuation">\</span>Recorders<span class="token punctuation">\</span>ReverbConnections</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Reverb<span class="token punctuation">\</span>Pulse<span class="token punctuation">\</span>Recorders<span class="token punctuation">\</span>ReverbMessages</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token string single-quoted-string">'recorders'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token class-name static-context">ReverbConnections</span><span class="token operator">::</span><span class="token keyword">class</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'sample_rate'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">ReverbMessages</span><span class="token operator">::</span><span class="token keyword">class</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'sample_rate'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，将每个记录器的 Pulse 卡片添加到你的 <a href="https://learnku.com/docs/laravel/11.x/pulse#dashboard-customization" target="_blank" rel="noopener noreferrer">Pulse 面板</a>:</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-pulse&gt;</span>
<span class="line">    &lt;livewire:reverb.connections cols=&quot;full&quot; /&gt;</span>
<span class="line">    &lt;livewire:reverb.messages cols=&quot;full&quot; /&gt;</span>
<span class="line">    ...</span>
<span class="line">&lt;/x-pulse&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="在生产环境中运行-reverb" tabindex="-1"><a class="header-anchor" href="#在生产环境中运行-reverb"><span>在生产环境中运行 Reverb</span></a></h2>
<p>由于 WebSocket 服务器的长期运行特性，您可能需要对服务器和托管环境进行一些优化，以确保 Reverb 服务器能够有效地处理服务器上可用资源的最佳连接数。</p>
<blockquote>
<p>[!注意]<br>
如果您的网站由 <a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a> 管理，您可以直接从”应用程序”面板自动优化您的服务器以进行混响。通过启用混响集成，Forge 将确保您的服务器已准备好生产，包括安装任何所需的扩展和增加允许的连接数量。</p>
</blockquote>
<h3 id="打开的文件" tabindex="-1"><a class="header-anchor" href="#打开的文件"><span>打开的文件</span></a></h3>
<p>每个 WebSocket 连接都保存在内存中，直到客户端或服务器断开连接。在 Unix 和类 Unix 环境中，每个连接都由一个文件表示。但是，在操作系统和应用程序级别，通常对允许打开的文件数有限制。</p>
<h4 id="操作系统" tabindex="-1"><a class="header-anchor" href="#操作系统"><span>操作系统</span></a></h4>
<p>在基于 Unix 的操作系统上，您可以使用以下 <code v-pre>ulimit</code> 命令确定允许的打开文件数：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token builtin class-name">ulimit</span> <span class="token parameter variable">-n</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此命令将显示允许不同用户打开的文件限制。您可以通过编辑 <code v-pre>/etc/security/limits.conf</code> 文件来更新这些值。例如，将 <code v-pre>forge</code> 用户打开的文件的最大数更新为 10,000 个，如下所示：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token comment"># /etc/security/limits.conf</span></span>
<span class="line">forge        soft  nofile  10000</span>
<span class="line">forge        hard  nofile  10000</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="事件循环" tabindex="-1"><a class="header-anchor" href="#事件循环"><span>事件循环</span></a></h3>
<p>Reverb 在底层使用了 ReactPHP 事件循环来管理服务器上的 WebSocket 连接。默认情况下，这个事件循环由 <code v-pre>stream_select</code> 驱动，它不需要任何额外的扩展。然而，<code v-pre>stream_select</code> 通常限制为最多 1024 个打开的文件。因此，如果您计划处理超过 1000 个并发连接，您将需要使用其他不受同样限制的事件循环。</p>
<p>Reverb 将在可用时自动切换到 <code v-pre>ext-uv</code> 有源循环。此 PHP 扩展可通过 PECL 安装：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">pecl <span class="token function">install</span> uv</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="web-服务" tabindex="-1"><a class="header-anchor" href="#web-服务"><span>Web 服务</span></a></h3>
<p>在大多数情况下，Reverb 在服务器上不面向 Web 的端口上运行。因此，为了将流量路由到 Reverb，您应该配置反向代理。假设 Reverb 在主机 <code v-pre>0.0.0.0</code> 和端口 <code v-pre>8080</code> 上运行，并且您的服务器使用 Nginx Web 服务器，则可以使用以下 Nginx 站点配置为您的 Reverb 服务器定义反向代理：</p>
<div class="language-nginx line-numbers-mode" data-highlighter="prismjs" data-ext="nginx" data-title="nginx"><pre v-pre class="language-nginx"><code><span class="line"><span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span></span>
<span class="line">    ...</span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_http_version</span> 1.1</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> Host <span class="token variable">$http_host</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> Scheme <span class="token variable">$scheme</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> SERVER_PORT <span class="token variable">$server_port</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> REMOTE_ADDR <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> Upgrade <span class="token variable">$http_upgrade</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> Connection <span class="token string">"Upgrade"</span></span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_pass</span> http://0.0.0.0:8080</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    ...</span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通常，Web 服务器配置为限制允许的连接数，以防止服务器过载。要将 Nginx Web 服务器上允许的连接数增加到 10,000 个，应更新 <code v-pre>nginx.conf</code> 文件 <code v-pre>worker_rlimit_nofile</code> 的 和 <code v-pre>worker_connections</code> 值：</p>
<div class="language-nginx line-numbers-mode" data-highlighter="prismjs" data-ext="nginx" data-title="nginx"><pre v-pre class="language-nginx"><code><span class="line"><span class="token directive"><span class="token keyword">user</span> forge</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token directive"><span class="token keyword">worker_processes</span> auto</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token directive"><span class="token keyword">pid</span> /run/nginx.pid</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token directive"><span class="token keyword">include</span> /etc/nginx/modules-enabled/*.conf</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token directive"><span class="token keyword">worker_rlimit_nofile</span> <span class="token number">10000</span></span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token directive"><span class="token keyword">events</span></span> <span class="token punctuation">{</span></span>
<span class="line">  <span class="token directive"><span class="token keyword">worker_connections</span> <span class="token number">10000</span></span><span class="token punctuation">;</span></span>
<span class="line">  <span class="token directive"><span class="token keyword">multi_accept</span> <span class="token boolean">on</span></span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上述配置将允许每个进程生成多达 10,000 个 Nginx 工作线程。此外，此配置将 Nginx 的打开文件限制设置为 10,000。</p>
<h3 id="端口" tabindex="-1"><a class="header-anchor" href="#端口"><span>端口</span></a></h3>
<p>基于 Unix 的操作系统通常会限制服务器上可以打开的端口数。您可以通过以下命令查看当前允许的范围：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"> <span class="token function">cat</span> /proc/sys/net/ipv4/ip_local_port_range</span>
<span class="line"><span class="token comment"># 32768    60999</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>上面的输出显示服务器最多可以处理 28,231 （60,999 - 32,768） 个连接，因为每个连接都需要一个空闲端口。尽管我们建议使用 <a href="#scaling">水平扩展</a> 来增加允许的连接数，但您可以通过更新服务器 <code v-pre>/etc/sysctl.conf</code> 配置文件中允许的端口范围来增加可用开放端口的数量。</p>
<h3 id="进程管理" tabindex="-1"><a class="header-anchor" href="#进程管理"><span>进程管理</span></a></h3>
<p>在大多数情况下，您应该使用进程管理器（如 Supervisor）来确保 Reverb 服务器持续运行。如果使用 Supervisor 运行 Reverb， <code v-pre>supervisor.conf</code> 则应更新服务器文件 <code v-pre>minfds</code> 的设置，以确保 Supervisor 能够打开处理与 Reverb 服务器连接所需的文件：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token section"><span class="token punctuation">[</span><span class="token section-name selector">supervisord</span><span class="token punctuation">]</span></span></span>
<span class="line">...</span>
<span class="line"><span class="token key attr-name">minfds</span><span class="token punctuation">=</span><span class="token value attr-value">10000</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="扩展" tabindex="-1"><a class="header-anchor" href="#扩展"><span>扩展</span></a></h3>
<p>如果需要处理的连接数超过单个服务器允许的连接数，则可以水平扩展 Reverb 服务。利用 Redis 的发布/订阅功能，Reverb 能够管理跨多个服务的连接。当应用程序的某个 Reverb 服务收到消息时，该服务将使用 Redis 将传入消息发布到所有其他服务器。</p>
<p>若要启用水平扩展，应在应用程序的 <code v-pre>.env</code> 配置文件中将 <code v-pre>REVERB_SCALING_ENABLED</code> 环境变量设置为 <code v-pre>true</code> ：</p>
<div class="language-env line-numbers-mode" data-highlighter="prismjs" data-ext="env" data-title="env"><pre v-pre class="language-env"><code><span class="line">REVERB_SCALING_ENABLED=true</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，您应该有一个专门的 Redis 服务器，与所有的 Reverb 服务通信。Reverb 将使用<a href="https://learnku.com/docs/laravel/11.x/redis#configuration" target="_blank" rel="noopener noreferrer">应用程序配置的默认 Redis 连接</a>将消息发布到所有 Reverb 服务器。</p>
<p>一旦您启用 Reverb 的拓展选项并配置 Redis 服务器后，只需在能够与 Redis 服务器通信的多个服务器上调用该 <code v-pre>reverb:start</code> 命令即可。这些 Reverb 服务应放置在负载均衡器后面，该负载均衡器在服务之间均匀分配传入请求。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/reverbmd/16730" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/re...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/reverbmd/16730" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/re...</a></p>
</blockquote>
</div></template>


